<?php 
	namespace Home\Model;
	use Think\Model;

	class ProgramDownloadModel extends Model
	{
	 	protected $trueTableName = "t_download";

	 	public function get_movie($program_id)
	 	{	
	 		// dump($program_id);exit;   
	 		$program_ids = explode(",", $program_id);
	 		foreach ($program_ids as $key => $value) {
	 			 $url_res[] = $this->get_url($value);
	 		}
	 		return $url_res?$this->send_url($url_res):"";
	 	}

	 	private function send_url($url_res)
		{
			// dump($url_res);
			// $current_url = array_shift($url_res);
			// if($current_url != ""){
			// 	 $res = $this->download($current_url,$url_res);
			// }
			foreach ($url_res as $key => $value) {
				$res = $this->download($value);
				// while ($res) {
				// 	$this->download($value);
				// }
			}
		}

	 	private function get_url($program_id)
	 	{
	 		$map['program_id'] = $program_id;
	 		$movie_ids = $this->where($map)->select();  
	 		foreach ($movie_ids as $key => $value) {
	 			$ids[] = $value["download_id"];
	 		}
	 		// 视频表
	 		$map1["id"] = array("in",$ids);
	 		// $map1["status"] = 1;   //暂定为状态为1的视频下载
	 		$movies = D("Download")->where($map1)->select();    //下载的视频数组
	 		// dump($movies);exit;
	 		// 取出视频地址	
	 		foreach ($movies as $key => $value) {
	 			$url = $value["download_url"];  
	 		}
	 		return $url;
	 	}

	 	/*private function download($url,$loaction_src)
	 	{
	 		$url = "ftp://ftpuser:123456@121.196.216.15/".$url;
	 		// dump($url);dump($loaction_src);exit;
	 		set_time_limit(0); // 设置脚本执行时间无限长
	        if(is_file($loaction_src)){  //防止文件重复写入
	            unlink($loaction_src);
	        }
	        clearstatcache();
	        // self::$remote_file_size = filesize($loaction_src);
	        $fpsrc = fopen($url, "rb");
	        // dump($fpsrc);
	        if (!$fpsrc = fopen($url, "rb"))
	        {
	            return false;
	        }
	     
	        $isWriteFileOpen = false; // 写文件 是否已打开？
	        do
	        {
	            $data = fread($fpsrc, 8192); // 每次读取 8*1024个字节
	            if (!$data)
	            {
	                break;
	            }else if (!$isWriteFileOpen)
	            {
	                // 第一次读取文件，并且有内容，才创建文件
	                $fpDst = fopen($loaction_src, "wb");
	                $isWriteFileOpen = true;
	                fwrite($fpDst, $data);
	            }else
	            {
	                // 写入
	                fwrite($fpDst, $data);
	            }
	        } while (true);
	     
	        fclose($fpsrc);
	        fclose($fpDst);
	        return true;
	 	}*/

	 	private function download($url){
	 		$url = "/home/ftp/".$url;
	        $filename = basename($url);
	        if(!file_exists($url)){//检测文件是否存在
	            echo "文件不存在！";
	            die();
	        }
	        $fp=fopen($url,'r');//只读方式打开
	        $filesize=filesize($url);//文件大小

	        //返回的文件(流形式)
	        header("Content-type: application/octet-stream");
	        //按照字节大小返回
	        header("Accept-Ranges: bytes");
	        //返回文件大小
	        header("Accept-Length: $filesize");
	        //这里客户端的弹出对话框，对应的文件名
	        header("Content-Disposition: attachment; filename=".$filename);
	        ob_clean();
	        flush();
	        //设置分流
	        $buffer=1024;
	        $count=0;
	        while(!feof($fp)&&($filesize-$count>0)){
	            $data=fread($fp,$buffer);
	            $count += $data;//计数
	            echo $data;     //传数据给浏览器端
	        }
	        fclose($fp);   		
	        return true;
	    }
	}